<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增任务')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: jasny-bootstrap-css" />
    <style type='text/css'>
        #tagbox div{
            display: inline-block;
            max-width: 100px;
            padding: 3px 8px 3px 8px;
            margin-bottom: 10px;
        }
        #tagbox div button{
            right: -5px;
        }
    </style>
</head>
<body style="background-color: #F8F8F8">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content" style="background-color: #F8F8F8">
        <form class="form-horizontal m" id="form-taskcenter-add">
            <div class="col-md-7" style="background-color: white;padding-top: 30px;margin-right: 3.3%">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">任务标题：</label>
                    <div class="col-sm-9">
                        <input name="taskTitle" class="form-control" type="text" maxlength="30" required>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label  is-required">任务详情：</label>
                    <div class="col-sm-9">
                        <input type="hidden" class="form-control" name="taskContent" required>
                        <div class="summernote" id="taskContent"></div>
                    </div>
                </div>
            </div>
            <div class="col-md-4" style="background-color: white;padding-top: 30px;min-height: 400px">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">任务标签：</label>
                    <div class="col-sm-8">
                        <div class="input-group " style="width: 100%;">
                            <input name="tag" type="text" placeholder="学习，java，it" class="form-control" id="baidu" maxlength="30" required>
                            <div class="input-group-btn">
                                <button type="button" class="btn btn-white dropdown-toggle" style="height: 31px" data-toggle="dropdown">
                                    <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required" >结束时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="finishDate" class="form-control" readonly  type="text" required>
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">任务封面：</label>
                    <div class="col-sm-8">
                        <div class=" fileinput fileinput-new input-group" data-provides="fileinput">
                            <div class="form-control" data-trigger="fileinput"><i class="glyphicon glyphicon-file fileinput-exists"></i> <span class="fileinput-filename ellipsis"></span></div>
                            <span class="input-group-addon btn btn-white btn-file"><span class="fileinput-new">选择文件</span><span class="fileinput-exists">更改</span><input id="taskThumbnailImg" name="taskThumbnailImg" type="file" required><input class="hidden" id="taskThumbnail" name="taskThumbnail" type="text"> </span>
                            <a href="#" class="input-group-addon btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">任务简介：</label>
                    <div class="col-md-8">
                        <textarea maxlength="50" style="margin-left: 15px;"  id="taskSummary" name="taskSummary"
                                  placeholder="请输入任务简介" rows="3" required></textarea>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <th:block th:include="include :: bootstrap-suggest-js" />
    <script th:inline="javascript">
        var prefix = ctx + "taskcenter/taskcenter"
        $("#form-taskcenter-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            console.log("123")
            if ($.validate.form()) {
                console.log("456")
                var file = $('#taskThumbnailImg')[0].files[0];
                if (/^image\/\w+$/.test(file.type)) {
                    var data = new FormData();
                    data.append("file", file);
                    $.ajax({
                        type: "post",
                        url: ctx + "common/upload",
                        data: data,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function(result) {
                            if (result.code === 0){
                                $('#taskThumbnail').val(result.url)
                                $.operate.save(prefix + "/add", $('#form-taskcenter-add').serialize());
                            }else {
                                $.modal.alertWarning('图片上传失败！');
                            }
                        }
                    })
                } else {
                    $.modal.alertWarning('请选择一个图片文件！');
                }

            }
        }

        $("input[name='finishDate']").datetimepicker({
            format: "yyyy-mm-dd",
            startDate: new Date(),
            minView: "month",
            autoclose: true
        });


        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                dialogsInBody: true,
                window:200,
                minHeight: 300,
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                    	var data = new FormData();
                    	data.append("file", files[0]);
                    	$.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                    		data: data,
                    		cache: false,
                    		contentType: false,
                    		processData: false,
                    		dataType: 'json',
                    		success: function(result) {
                    		    if (result.code == web_status.SUCCESS) {
                    		        $('#' + obj.id).summernote('insertImage', result.url);
                    		    } else {
                    		        $.modal.alertError(result.msg);
                    		    }
                    		},
                    		error: function(error) {
                    		    $.modal.alertWarning("图片上传失败。");
                    		}
                    	});
                    }
                }
            });
        });
        //百度搜索测试
        var baiduBsSuggest = $("#baidu").bsSuggest({
            allowNoKeyword: false, //是否允许无关键字时请求数据
            multiWord: true, //以分隔符号分割的多关键字支持
            separator: ",", //多关键字支持时的分隔符，默认为空格
            getDataMethod: "url", //获取数据的方式，总是从 URL 获取
            //url: 'http://unionsug.baidu.com/su?p=3&t=' + (new Date()).getTime() + '&wd=',
            url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?json=1' + '&wd=',
            /*优先从url ajax 请求 json 帮助数据，注意最后一个参数为关键字请求参数*/
            jsonp: 'cb',
            /*如果从 url 获取数据，并且需要跨域，则该参数必须设置*/
            processData: function (json) { // url 获取数据时，对数据的处理，作为 getData 的回调函数
                var i, len, data = {
                    value: []
                };
                if (!json || !json.s || json.s.length === 0) {
                    return false;
                }

                console.log(json);
                len = json.s.length;

                jsonStr = "{'value':[";
                for (i = 0; i < len; i++) {
                    data.value.push({
                        word: json.s[i]
                    });
                }
                data.defaults = 'baidu';

                //字符串转化为 js 对象
                return data;
            }
        });
    </script>
</body>
<style>
    /*清楚浮动*/
    .clearfix:after {
        content: '';
        display: block;
        clear: both;
    }

    .tile .form-group label {
        font-weight: 700;
    }
    /*显示不全用...表示*/
    .ellipsis {
        width: 40px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: inline-block;
    }
</style>
</html>